package com.guxingke.gblog.action;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.guxingke.gblog.util.PictureUtil;
import com.guxingke.gblog.util.PropertiesUtil;
import com.opensymphony.xwork2.ActionSupport;

public class PictureManageAction extends ActionSupport implements
		ServletRequestAware {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private String[] userImages;
	private String[] articleImages;
	private String mainPage;

	private File userImage;
	private File articleImage;

	private String userImageFileName;
	private String articleImageFileName;

	public File getUserImage() {
		return userImage;
	}

	public void setUserImage(File userImage) {
		this.userImage = userImage;
	}

	public File getArticleImage() {
		return articleImage;
	}

	public void setArticleImage(File articleImage) {
		this.articleImage = articleImage;
	}

	public String getUserImageFileName() {
		return userImageFileName;
	}

	public void setUserImageFileName(String userImageFileName) {
		this.userImageFileName = userImageFileName;
	}

	public String getArticleImageFileName() {
		return articleImageFileName;
	}

	public void setArticleImageFileName(String articleImageFileName) {
		this.articleImageFileName = articleImageFileName;
	}

	@Override
	public String execute() throws Exception {
		String imageBasePath = request.getSession().getServletContext()
				.getRealPath("image");
		userImages = PictureUtil.getPictures(imageBasePath,
				PropertiesUtil.getValue("userPicturePath"));
		articleImages = PictureUtil.getPictures(imageBasePath,
				PropertiesUtil.getValue("articlePicturePath"));
		mainPage = "template/picture/detail.jsp";
		return SUCCESS;
	}

	public String upload() throws Exception {
		String imageBasePath = request.getSession().getServletContext()
				.getRealPath("image");

		if (userImage != null) {
			String userImagePath = imageBasePath + File.separator + "user"
					+ File.separator + userImageFileName;
			File saveFile1 = new File(userImagePath);
			FileUtils.copyFile(userImage, saveFile1);
		}

		if (articleImage != null) {
			String articleImagePath = imageBasePath + File.separator
					+ "article" + File.separator + articleImageFileName;
			File saveFile2 = new File(articleImagePath);
			FileUtils.copyFile(articleImage, saveFile2);
		}

		userImages = PictureUtil.getPictures(imageBasePath,
				PropertiesUtil.getValue("userPicturePath"));
		articleImages = PictureUtil.getPictures(imageBasePath,
				PropertiesUtil.getValue("articlePicturePath"));
		mainPage = "template/picture/detail.jsp";
		return SUCCESS;
	}

	public String[] getUserImages() {
		return userImages;
	}

	public void setUserImages(String[] userImages) {
		this.userImages = userImages;
	}

	public String[] getArticleImages() {
		return articleImages;
	}

	public void setArticleImages(String[] articleImages) {
		this.articleImages = articleImages;
	}

	public String getMainPage() {
		return mainPage;
	}

	public void setMainPage(String mainPage) {
		this.mainPage = mainPage;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
}
